<template>
	<view class="page-wrap">
		<navBar></navBar>
		<match-media v-if="smallScreen" :max-width="MAX_SIZEiZE - 1">
			<view class="tabbar">
				<tabBar :showselected="true"></tabBar>
			</view>
		</match-media>
		<view class="content">
			<match-media v-if="smallScreen" :max-width="MAX_SIZEiZE - 1">
				<uni-nav-bar fixed statusBar backgroundColor="var(--bg-3)" :border="false">
					<text class="page-title">{{$t('Page.menu.notify')}}</text>
				</uni-nav-bar>
			</match-media>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" :style="{height:scrollHeight}" class="scroll-Y"
				@scroll="onScroll">
				<view class="slot_list">
					<view class="slot_item" v-for="(item,index) in activeList" :key="index"
						@click="toDetailPage(item.id)">
						<image class="active-image" :src="item.image"></image>
						<view class="item-right">
							<view class="item-title">
								{{item.title}}
							</view>
							<view class="item-cont">
								{{item.content}}
							</view>
						</view>
					</view>
				</view>
				<view class="loadMoreWrap">
					<uni-load-more :status="status" :contentText="contentText"></uni-load-more>
				</view>
			</scroll-view>
		</view>
		<backToTop :isShow="isShow" @scrollToTop="scrollToTop" :safeArear="true"></backToTop>
	</view>
</template>
<script>
	import {
		MAX_SIZEiZE
	} from '@/common/app.js'
	import {
		mapState,
		mapGetters
	} from 'vuex'
	import {
		Debounce
	} from '@/utils/common.js'
	import {
		sendMessage
	} from '@/api/websocket.js'
	export default {
		data() {
			return {
				MAX_SIZEiZE,
				scrollTop: 0,
				searchParams: {
					order: '2',
					page: 1,
					page_ini: 20
				},
				activeList: [],
				totalCount: 0,
				status: 'more', //  more/loading/noMore
				contentText: {
					contentdown: this.$t('Page.loadmore.xl'),
					contentrefresh: this.$t('Page.loadmore.jzz'),
					contentnomore: this.$t('Page.loadmore.mygdl')
				},
				intersectionObserver: null,
				maxPage: 0,
				triggerHeight: Math.ceil(uni.getSystemInfoSync().windowHeight / 2.5),
				isShow: false
			}
		},
		watch: {
			totalCount: {
				handler(newVal) {
					if (newVal) {
						const remainder = this.totalCount % this.searchParams.page_ini
						const countPage = this.totalCount / this.searchParams.page_ini
						this.maxPage = remainder === 0 ? countPage : Math.floor(countPage) + 1
						this.initIntersectionObserver()
					}
				}
			}
		},
		computed: {
			...mapState({
				theme: state => state.app.theme,
				windowSize: state => state.app.windowSize
			}),
			...mapGetters('app', ['isLogin', 'bigScreen', 'smallScreen']),
			scrollHeight() {
				const statusBarHeight = uni.getSystemInfoSync().statusBarHeight
				if (this.windowSize.windowWidth <= 768) {
					return `calc(100vh - 44px - 50px - 20px - ${statusBarHeight}px)`
				} else {
					return 'calc(100vh - 70px - 30px)'
				}
			},
		},
		onPullDownRefresh() {
			this.activeList = [];
			this.searchParams.page = 1;
			this.getActiveList();
		},
		onLoad() {
			this.getActiveList();
		},
		methods: {
			onScroll: Debounce(function(e) {
				this.scrollTop = e.detail.scrollTop
				if (e.detail.scrollTop >= this.triggerHeight) {
					this.isShow = true
				} else {
					this.isShow = false
				}
			}, 500),
			scrollToTop(e) {
				this.scrollTop = 0
				this.isShow = false
			},
			initIntersectionObserver() {
				this.intersectionObserver = uni.createIntersectionObserver(this);
				this.intersectionObserver.relativeTo('.page-wrap').observe('.loadMoreWrap', (res) => {
					console.log('this.searchParams.page < this.maxPage: ', this.searchParams.page < this.maxPage);
					if (res.intersectionRatio > 0 && this.searchParams.page < this.maxPage) {
						if (this.searchParams.page >= 1) {
							this.searchParams.page++
						}
						this.getActiveList();
					}
				})
			},
			// lower(e) {
			// 	if (this.activeList.length < this.totalCount) {
			// 		this.status = 'loading'
			// 		this.searchParams.page++
			// 		this.getActiveList()
			// 	}
			// },
			getActiveList() {
				this.status = 'loading'
				const {
					ARTICLE
				} = this.$api
				sendMessage({
					flag: ARTICLE,
					content: this.searchParams
				})
				uni.$once(ARTICLE, (res) => {
					const {
						code,
						data
					} = res
					if (code === 1) {
						if (this.searchParams.page === 1) {
							this.activeList = data.list
							this.totalCount = data.count
						} else {
							this.activeList = [...this.activeList, ...data.list]
						}
					}

					if (this.activeList.length < this.totalCount) {
						setTimeout(() => {
							this.status = 'more'
						}, 1000)
					} else {
						this.status = 'noMore'
					}
				})
			},
			toDetailPage(id) {
				uni.navigateTo({
					url: `/pages/Notify/notifyDetail?id=${id}`
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	// 默认样式 结构   
	.page-wrap {
		background: var(--bg-3);

		.content {
			width: 100%;
			min-height: 100vh;
			background: var(--bg-3);
			padding: 0 15px 0;
			box-sizing: border-box;

			.page-title {
				width: 100%;
				margin: auto;
				height: 44px;
				line-height: 44px;
				font-size: 20px;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				text-align: center;
			}

			.scroll-Y {
				margin-top: 15px;

				/* 隐藏竖向滚动条 */
				::-webkit-scrollbar {
					display: none;
				}

				.slot_list {
					display: grid;
					grid-template-columns: repeat(auto-fill, minmax(345px, 1fr));
					/* 三列等宽 */
					gap: 15px;

					.slot_item {
						width: 100%;
						display: flex;
						align-items: center;
						padding: 15px;
						border-radius: 15px;
						background: var(--bg-20);
						overflow: hidden;
						box-sizing: border-box;
						cursor: pointer;
						border: 2px solid transparent;


						&:hover {
							opacity: .65;
						}

						.active-image {
							width: 90px;
							height: 90px;
							border-radius: 10px;
						}

						.item-right {
							flex: 1;
							height: 100%;
							box-sizing: border-box;
							word-break: break-all;
							margin-left: 15px;
							display: flex;
							flex-direction: column;

							.item-title {
								font-size: 16px;
								font-family: PingFang SC-Bold, PingFang SC;
								font-weight: bold;
								color: var(--color-25);
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 1;
								-webkit-box-orient: vertical;
							}

							.item-cont {
								font-size: 13px;
								font-family: PingFang SC-Medium, PingFang SC;
								color: var(--color-31);
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 3;
								-webkit-box-orient: vertical;
								margin-top: 4px;
							}
						}
					}
				}
			}
		}
	}

	// PC
	@media screen and (min-width: 768px) {

		.page-wrap {
			padding-top: 70px;

			.content {
				width: 100%;
				min-height: calc(100vh - 70px) !important;
				height: calc(100vh - 70px) !important;
				overflow-y: auto;
				background: var(--bg-3);
				padding: 30px 30px 0;
				box-sizing: border-box;

				.scroll-Y {
					margin-top: 0 !important;

					.slot_list {
						display: grid;
						grid-template-columns: repeat(auto-fill, minmax(345px, 1fr));
						/* 三列等宽 */
						gap: 30px !important;

						.active-image {
							width: 140px !important;
							height: 140px !important;
						}

						.item-cont {
							-webkit-line-clamp: 4 !important;
						}
					}
				}
			}
		}
	}
</style>